<template>
  <div>
    <div class="scan_content" @click="onscan()">
      <div class="scan_content_img">
        <img
          id="scan"
          src="@/assets/Images/AppIcon/icon_scan_company.png"
          alt=""
        />
      </div>
      <div class="scan_content_alert">
        <span>您需要扫描公司二维码</span>
      </div>
    </div>
  </div>
</template>

<script>
import { loadWxTicket, loadWxWorkTicket } from "@/utils/Ticket.js";
import { qrCodeVerify } from "@/api/public.js";
import { Toast } from "vant";
import { register } from "@/api/PAPublic";
var wait = 60;
export default {
  data() {
    return {
      timestamp: "",
      signature: "",
      nonceStr: "",
      ticket: "",
      code: "",
      qrCode: "",
      mobile: "",
    };
  },
  created() {
    if (this.$route.query.qrCode) {
      this.qrCode = this.$route.query.qrCode;
    }
    if (this.$route.query.mobile) {
      this.mobile = this.$route.query.mobile;
    }
    if (this.$route.query.verifyCode) {
      this.verifyCode = this.$route.query.verifyCode;
    }
  },
  mounted() {
    // 页面加载完成唤醒微信扫一扫
    this.getSign(["scanQRCode"])
    //loadWxTicket(["scanQRCode"]).then((data)=>{console.log(data)}).catch((err) => {});
  },
  methods: {
    getSign(arr){
      let params = {
        url: window.location.href.split("#")[0],
      };
      // this.$axios.get("/WxOpen/GetSignature",{params:params})
      this.$axios.get("/WxWork/GetSignature",{params:params})
      .then((res)=>{
         console.log("调用成功")
        wx.config({
          beta: true, // 必须这么写，否则wx.invoke调用形式的jsapi会有问题
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          appId: res.data.appId, // 必填，公众号的唯一标识
          timestamp: res.data.timestamp, // 必填，生成签名的时间戳
          nonceStr: res.data.nonceStr, // 必填，生成签名的随机串
          signature: res.data.signature, // 必填，签名，见附录1
          jsApiList: arr, // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
        }); 
        wx.ready(() => {
          wx.checkJsApi({
            jsApiList: arr, // 需要检测的JS接口列表，所有JS接口列表见附录2,
            success: function (res) {
              arr.forEach((a) => {
                console.log("调用JS方法[" + a + "]的微信tick成功！"+JSON.stringify(res))
              })
            }
          });
        })
        wx.error((err) => {
          console.log("错误"+err)
        });
      })
    },
    onscan() {
      console.log("进入扫描方法")
      const xmobile = this.mobile;
      const xverifyCode = this.verifyCode;
      const xqrCode = this.qrCode;
      wx.ready(function() {
        console.log("开始扫描")
        wx.scanQRCode({
          needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
          scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
          success: function(res) {
            console.log("扫描成功")
            if (res.resultStr) {
              qrCodeVerify({ code: res.resultStr }).then((r) => {
                if (r.data.Data) {
                  var qrEntity = JSON.parse(r.data.Data);
                  if((r.data.CodeType!="Company"&&!qrEntity.CompanyCode)){
                    Toast.fail("扫描的二维码无公司信息，请扫描公司码");
                  }
                  else if (qrEntity.Code) {
                    window.location.href =
                      "#/registerInfo?qrCode=" +
                      xqrCode +
                      "&mobile=" +
                      xmobile +
                      "&verifyCode=" +
                      xverifyCode;
                  } else {
                    Toast.fail("无效的二维码");
                  }
                } else {
                  Toast.fail("无效的二维码");
                }
              });
            } else {
              Toast.fail("无效的二维码");
            }
          },
          error: function(res) {
            Toast("出错了");
          },
        });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.phone {
  position: relative;
  .code {
    position: absolute;
    right: 20px;
    top: 20px;
    font-size: 12px;
  }
}

.scan_content {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 14px;
  background-color: #383d40;
  height: 6rem;
  align-items: center;
  justify-content: center;
  .scan_content_img {
    display: flex;
    flex: 4;
    align-items: center;
    justify-content: center;
    img {
      width: 2rem;
      height: 2rem;
    }
  }
  .scan_content_alert {
    display: flow-root;
    flex: 1;
    align-items: center;
    justify-content: center;
    color: #fff;
  }
}
</style>
